using B.S.Sterilizationcenter.Domain.yjb;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Yjb.PrescriptionCheck;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Yjb.PrescriptionCheck
{
    public class DelPrescriptionCheckCommandHandler : IRequestHandler<DelPrescriptionCheckCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DelPrescriptionCheckCommandHandler(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(DelPrescriptionCheckCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();

                var del = await db.Queryable<Domain.yjb.PrescriptionCheck>().FirstAsync(p => p.PrescriptionCheckId == request.PrescriptionCheckId);
                if (del != null)
                {
                    del.IsDeleted = true;
                    var res = await db.Updateable<Domain.yjb.PrescriptionCheck>(del).ExecuteCommandAsync();
                    if (res > 0)
                    {
                        result.Code = ApiEnums.删除成功;
                        result.Msg = "删除成功";
                    }
                    else
                    {
                        result.Code = ApiEnums.删除失败;
                        result.Msg = "删除失败";
                    }
                }
                else
                {
                    result.Code = ApiEnums.删除失败;
                    result.Msg = "记录不存在";
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
} 